﻿<%@ Page Language="C#" AutoEventWireup="true" Culture="en-GB" CodeFile="ClientLedgers.aspx.cs"
    Inherits="Main_Reports_ServiceInvoice_InvoiceViews_ClientLedgers" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>
<%@ Register Src="~/User Controls/PageHeading.ascx" TagName="heading" TagPrefix="acc" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Advance Receipts Register</title>
    <link href="../../../../Styles/accounts.css" rel="stylesheet" type="text/css" />
    <style type="text/css">
        .gridheader
        {
            margin-left: 3px;
            margin-right: 3px;
        }
    </style>

    <script type="text/javascript" language="javascript">
        function checkdate() {
            if (document.getElementById("txtfrom").value == '') {
                alert('Specify From date!');
                document.getElementById("txtfrom").focus();
                return false;
            }
            else if (document.getElementById("txtto").value == '') {
                alert('Specify To Date!');
                document.getElementById("txtto").focus();
                return false;
            }
            else {
                var strFromDate = document.getElementById("txtfrom").value;
                var strToDate = document.getElementById("txtto").value;
                var dtFrom = parseInt(strFromDate.substring(0, 2), 10);
                var monFrom = parseInt(strFromDate.substring(3, 5), 10);
                var yrFrom = parseInt(strFromDate.substring(6, 10), 10);
                var dtTo = parseInt(strToDate.substring(0, 2), 10);
                var monTo = parseInt(strToDate.substring(3, 5), 10);
                var yrTo = parseInt(strToDate.substring(6, 10), 10);
                var dateFrom = new Date(yrFrom, monFrom, dtFrom);
                var dateTo = new Date(yrTo, monTo, dtTo);
                if (dateTo < dateFrom) {
                    alert("from date cannot be greater than to date");
                    document.getElementById("txtfrom").focus();
                    return false;
                }
                else {
                    return true;
                }
            }
        }
        function EnabledTabKey(id, e) {
            var keycode;
            if (window.event) {
                keycode = window.event.keyCode;
            }
            else if (e) {
                keycode = e.which;
            }
            else {
                return true;
            }

            if (keycode == 9) {
                // if is the tab key                
                return true;
            }
            else {
                return false;
            }
        }
        function Validation() {
            var flag = true;
            var txtFromdate = document.getElementById("txtFromDate").id;
            var txtTodate = document.getElementById("txtToDate").id;
            if (document.getElementById("txtFromDate").value == '') {
                alert('Specify From date');
                flag = false;
            }
            else if (document.getElementById("txtToDate").value == '') {
                alert('Specify To date');
                flag = false;
            }
            else {
                if (isDate(txtFromdate) && isDate(txtTodate)) {
                    var dateArray = new Array();
                    dateArray = document.getElementById("txtFromDate").value.split("/");
                    var day1 = dateArray[0];
                    var month1 = dateArray[1];
                    var year1 = dateArray[2];
                    dateArray = document.getElementById("txtToDate").value.split("/");
                    var day2 = dateArray[0];
                    var month2 = dateArray[2];
                    var year2 = dateArray[2];

                    if (day1.length < 2) {
                        day1 = '0' + day1;
                    }
                    if (month1.length < 2) {
                        month1 = '0' + month1;
                    }
                    if (day2.length < 2) {
                        day2 = '0' + day2;
                    }
                    if (month2.length < 2) {
                        month2 = '0' + month2;
                    }
                    var date1 = new Date(year1, month1 - 1, day1);
                    var date2 = new Date(year2, month2 - 1, day2);
                    if (date1 > date2) {
                        alert('Date Could Not be less than From Date');
                        document.getElementById(txtFromdate).focus();
                        flag = false;

                    }
                }
                else {
                    flag = false;
                }
            }
            return flag;
        }
        function isDate(txtDateId) {
            // date length should be 10 characters - no more, no less            
            var Ctrl = document.getElementById(txtDateId);
            var txtDate = Ctrl.value;
            var flag = true;

            // extract day, month and year from the txtDate string
            // expected format is dd/mm/yyyy
            var day = '';
            var month = '';
            var year = '';
            var dateArray = new Array();
            var count = 0;
            for (var j = 0; j < txtDate.length; j++) {
                var chara = txtDate.charAt(j);
                if (chara == '/') {
                    count += 1;
                }
            }
            if (count != 2) {
                alert('Invalid date !');
                return false;
            }
            dateArray = txtDate.split("/");
            day = dateArray[0];
            month = dateArray[1];
            year = dateArray[2];


            if (year.length != 4) {
                alert('Invalid date !');
                return false;
            }
            var finYear = document.getElementById("hidFinyear").value;
            if (month >= 4 && year != parseInt(finYear)) {
                alert('Date Should be between 01/04/' + finYear + ' and 31/03/' + (parseInt(finYear) + 1));
                return false;
            }
            else if (month < 4 && year != (parseInt(finYear) + 1)) {
                alert('Date Should be between 01/04/' + finYear + ' and 31/03/' + (parseInt(finYear) + 1));
                return false;
            }



            if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
                if (day > 31) {
                    alert('Date Should not be Greater than 31');
                    Ctrl.focus();
                    flag = false;
                }
            }
            //for have only 30 day
            else if (month == 4 || month == 6 || month == 9 || month == 11) {
                if (day > 30) {
                    alert('Date Should not be Greater than 30');
                    Ctrl.focus();
                    flag = false;
                }
            }
            //for Febuary 
            else if (month == 2) {
                var leap = year % 4;
                if (leap == 0) {
                    if (day > 29) {
                        alert('february have only 29 days');
                        Ctrl.focus();
                        flag = false;
                    }
                }
                else {
                    if (day > 28) {
                        alert('february have only 28 days');
                        Ctrl.focus();
                        flag = false;
                    }
                }
            }
            else if (month > 12) {
                alert('Invalid Date');
                Ctrl.focus();
                flag = false;
            }
            // otherwise return true
            return flag;
        }
        function printPartOfPage() {
            var printDiv = document.getElementById("pnlPrint").innerHTML;
            var windowUrl = 'about:blank';
            var uniqueName = new Date();
            var windowName = 'Print' + uniqueName.getTime();
            var printWindow = window.open(windowUrl, windowName, 'width=10,height=10');
            printWindow.document.write('<html><link rel="stylesheet" type="text/css" href="../../../../Styles/accounts.css" /><body>');
            printWindow.document.write(printDiv);
            printWindow.document.write("</body></html>");
            printWindow.document.close();
            printWindow.focus();
            printWindow.print();
            printWindow.close();
            return false;
        }
    </script>

    <style type="text/css">
        .style1
        {
            width: 100%;
        }
        .style2
        {
            width: 372px;
        }
    </style>

    <script type="text/javascript">
        function expandDetailsInGrid(imgId, trId) {
            var imgelem = document.getElementById(imgId);
            var rowdetelem = document.getElementById(trId);
            if (imgelem.alt == "plus") {
                imgelem.src = "../../../../Images/minus.png";
                imgelem.alt = "minus";
                if (navigator.appName == "Microsoft Internet Explorer") {
                    rowdetelem.style.display = "block";
                }
                else {
                    rowdetelem.style.display = "table-row";
                }
            }
            else {
                imgelem.src = "../../../../Images/plus.png";
                imgelem.alt = "plus";
                rowdetelem.style.display = "none";
            }
            return false;
        }
    </script>

</head>
<body>
    <form id="form2" runat="server">
    <asp:ScriptManager ID="scriptmanager2" runat="server">
    </asp:ScriptManager>
    <asp:HiddenField ID="hidfinyear" runat="server" />
        <acc:heading ID="pagehead" runat="server" HeadingText="Client Ledgers" />

    <asp:Panel ID="pnlTDSClient" runat="server">
        <div  style="width: 400px; margin-left:220px; margin-top: 20px;">
            <asp:GridView ID="grdTDSClient" runat="server" CssClass="gridview" DataKeyNames="LID"
                AlternatingRowStyle-CssClass="gridview_alternate" Width="100%" AllowPaging="true"
                PageSize="14" AutoGenerateColumns="false" OnPageIndexChanging="grdTDSClient_PageIndexChanging">
                <Columns>
                    <asp:TemplateField ItemStyle-Width="20px" ItemStyle-HorizontalAlign="Center" HeaderText="Select">
                        <ItemStyle HorizontalAlign="Center" />
                        <ItemTemplate>
                            <asp:CheckBox ID="chkOpt" runat="server" onclick="CheckSelectAll();" />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField ItemStyle-Width="400px" HeaderText="Customer Name">
                        <ItemTemplate>
                            <asp:Label ID="lblLedger" runat="server" Text='<%#Eval("LNAME") %>'></asp:Label>
                            <asp:HiddenField ID="hidLID" runat="server" Value='<%#Eval("LID") %>' />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <EmptyDataTemplate>
                    <center>
                        <span style="font-weight: bold; color: Red;">No Record Found !</span>
                    </center>
                </EmptyDataTemplate>
            </asp:GridView>
        </div>
        <div style="width: 400px; margin-left: 220px; margin-right: 20px;">
            <table width="100%" style="padding-top: 10px;">
                <tr>
                    <td align="left" style="padding-top: 10px;">
                        <asp:CheckBox ID="ChkSelectAll" runat="server" Text="Select All" onclick="SelectAll(this.id);" />
                    </td>
                    <td align="right" style="padding-top: 10px;">
                        <asp:Button ID="btnDisplay" runat="server" Text="Display" CssClass="button" OnClientClick="return checkselected();"
                            OnClick="btnDisplay_Click" />
                    </td>
                    <td align="left" style="padding-top: 10px;">
                        &nbsp;&nbsp;<asp:Button ID="Button1" runat="server" CssClass="button" Text="Cancel"
                            OnClick="btnCancel_Click" />
                    </td>
                </tr>
            </table>
        </div>
    </asp:Panel>
    <asp:Panel ID="pnlGrid" runat="server">
        <div style="width: 100%; margin-left: 10px;">
            <asp:HyperLink ID="hyperChangeDate" runat="server" NavigateUrl="#" Text="Change Date"></asp:HyperLink>&nbsp;&nbsp;&nbsp;<asp:HyperLink ID="hyplnkPrint" runat="server" NavigateUrl="#" onclick="return printPartOfPage();"
                        Text="Print"></asp:HyperLink>&nbsp;&nbsp;&nbsp;<asp:HyperLink ID="hyperMainMenu" runat="server" NavigateUrl="~/Main/Reports/ServiceInvoice/InvoiceViews/InvoiceView.aspx" Text="Main Menu"></asp:HyperLink>
            <br /><center>
            <b>
                <asp:Label ID="lblCompName2" runat="server"></asp:Label></b></center>
            <br />
            <center>
                <b>
                    <asp:Label ID="lblMsg" runat="server"></asp:Label></b></center>
        </div>
        <div class="tablecontent" style="width: 100%; margin-left: 10px;">
            <asp:GridView ID="grdClientLedgerView" CssClass="gridview" runat="server" AutoGenerateColumns="False"
                Width="100%" OnRowDataBound="grdClientLedgerView_RowDataBound" OnPageIndexChanging="grdClientLedgerView_PageIndexChanging"
                GridLines="Horizontal" ShowHeader="true">
                <Columns>
                    <asp:TemplateField ItemStyle-Width="100%">
                        <ItemTemplate>
                            <asp:ImageButton runat="server" ID="imgShow" ImageUrl="../../../../Images/plus.png"
                                AlternateText="plus" />
                            <asp:Label ID="lblCustomerName" runat="server" Font-Bold="true"></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField ItemStyle-Width="100%"></asp:TemplateField>
                </Columns>
                <EmptyDataTemplate>
                    Record Not Found !!
                </EmptyDataTemplate>
                <EmptyDataRowStyle ForeColor="Red" Font-Bold="true" />
            </asp:GridView>
        </div>
    </asp:Panel>
    <br />
    <br />
    <asp:Panel ID="pnlChangeDate" runat="server" Width="350px" Height="160px" Style="display: none"
        CssClass="modalPopup">
        <div style="width: 80%; margin: 20px 10% 10px 10%;">
            <table>
                <tr>
                    <td>
                        From Date
                    </td>
                    <td style="padding-left: 10px">
                        <asp:TextBox ID="txtFromDate" runat="server" CssClass="input" ToolTip="DD/MM/YYYY"
                            Width="100px"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        To Date
                    </td>
                    <td style="padding-left: 10px">
                        <asp:TextBox ID="txtToDate" runat="server" CssClass="input" ToolTip="DD/MM/YYYY"
                            Width="100px"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td colspan="2" align="center">
                        <asp:Button ID="btnChangeDate" runat="server" CssClass="button" Text="Ok" OnClick="btnChangeDate_Click"
                            OnClientClick="return Validation();" />&nbsp;
                        <asp:Button ID="btncancel" runat="server" CssClass="button" Text="Cancel" />&nbsp;
                    </td>
                </tr>
            </table>
        </div>
    </asp:Panel>
    <ajaxToolkit:ModalPopupExtender ID="modalChangeDate" runat="server" CancelControlID="btncancel"
        TargetControlID="hyperChangeDate" PopupControlID="pnlChangeDate" BackgroundCssClass="modalBackground">
    </ajaxToolkit:ModalPopupExtender>
     <asp:Panel ID="pnlPrint" Width="98%" runat="server" Style="margin-left: 1%; display:none;">
        <center>
            <b>
                <asp:Label ID="lblCompName" runat="server"></asp:Label></b></center>
        <br />
        <center>
            <b>
                <asp:Label ID="lblMsg2" runat="server"></asp:Label></b></center>
        <br />
        <asp:DataList ID="DataList2" OnItemDataBound="DataList2_ItemBound" Width="98%" ItemStyle-BorderStyle="Solid" ItemStyle-BorderWidth="1px" ItemStyle-BorderColor="#818181" SelectedItemStyle-VerticalAlign="Top" runat="server">        
        <ItemTemplate>           
                <b><asp:Label ID="lblPrintClientName" runat="server" Text='<%#Eval("LName") %>'></asp:Label></b><br />
                <asp:GridView ID="grdPrint" ShowFooter="false" DataKeyNames="Sortorder2" OnRowDataBound="grdPrint_RowDataBound" Width="100%" CssClass="gridview" style="margin-bottom:5px;" AutoGenerateColumns="false" runat="server">
                <Columns>                   
                    <asp:BoundField HeaderText="Particulars" DataField="Particulars" ItemStyle-Width="35%" />
                    <asp:BoundField HeaderText="Date" DataField="Date" DataFormatString="{0:dd-MMM-yyyy}" ItemStyle-Width="15%" ItemStyle-HorizontalAlign="Right" />
                    <asp:BoundField HeaderText="Debit Amt" DataField="Dr" ItemStyle-Width="15%" ItemStyle-HorizontalAlign="Right" />
                    <asp:BoundField HeaderText="Credit Amt" DataField="Cr" ItemStyle-Width="15%" ItemStyle-HorizontalAlign="Right" />
                    <asp:BoundField HeaderText="Balance Amt" DataField="Balance Amount" ItemStyle-Width="15%" ItemStyle-HorizontalAlign="Right" />                    
                    <asp:BoundField HeaderText="Cr/Dr" DataField="" ItemStyle-Width="5%" />                    
                </Columns>
                </asp:GridView>
        </ItemTemplate>
    </asp:DataList>
            </asp:Panel>
    </form>
</body>

<script type="text/javascript" language="javascript">
    function SelectAll(id) {

        //get reference of GridView control
        var grid = document.getElementById("<%= grdTDSClient.ClientID %>");

        //variable to contain the cell of the grid
        var cell;
        if (grid.rows.length > 0) {
            //loop starts from 1. rows[0] points to the header.
            for (i = 1; i < grid.rows.length; i++) {
                //get the reference of first column
                cell = grid.rows[i].cells[0];

                //loop according to the number of childNodes in the cell
                for (j = 0; j < cell.childNodes.length; j++) {
                    //if childNode type is CheckBox
                    if (cell.childNodes[j].type == "checkbox") {
                        //assign the status of the Select All checkbox to the cell checkbox within the grid
                        cell.childNodes[j].checked = document.getElementById(id).checked;
                    }
                }
            }
        }
    }
    function CheckSelectAll() {
        var countchecked = 0;

        //get reference of GridView control
        var grid = document.getElementById("<%= grdTDSClient.ClientID %>");

        //variable to contain the cell of the grid
        var cell;
        if (grid.rows.length > 0) {
            //loop starts from 1. rows[0] points to the header.
            for (i = 1; i < grid.rows.length; i++) {
                //get the reference of first column
                cell = grid.rows[i].cells[0];

                //loop according to the number of childNodes in the cell
                for (j = 0; j < cell.childNodes.length; j++) {
                    //if childNode type is CheckBox
                    if (cell.childNodes[j].type == "checkbox") {
                        //assign the status of the Select All checkbox to the cell checkbox within the grid
                        if (cell.childNodes[j].checked == true) {
                            countchecked = countchecked + 1;
                        }
                    }
                }
            }

            if (countchecked + 2 == grid.rows.length) {
                document.getElementById("ChkSelectAll").checked = true;
            }
            else {
                document.getElementById("ChkSelectAll").checked = false;
            }
        }
    }
    function checkselected() {
        var flag = true;
        var checkedcount = 0;
        var grid = document.getElementById("<%= grdTDSClient.ClientID %>");
        var cell;
        if (grid.rows.length > 0) {
            for (i = 1; i < grid.rows.length; i++) {
                cell = grid.rows[i].cells[0];
                for (j = 0; j < cell.childNodes.length; j++) {
                    if (cell.childNodes[j].type == "checkbox") {
                        if (cell.childNodes[j].checked == true) {
                            checkedcount = checkedcount + 1;
                        }
                    }
                }
            }
        }
        if (checkedcount <= 0) {
            alert("Please select a customer from list to display details !!");
            flag = false;
        }
        return flag;
    }
</script>

</html>
